//Where the LEDs at?
int fowardLed = 13;
int backLed = 12;// choose the pin for the LED

//Where the digital inputs at?
int PIN_0 = 2;
int PIN_1 = 3;
int PIN_2 = 4;

//We will store the reading from the digital pins in here later
int IN0 = 0;
int IN1 = 0;
int IN2 = 0;

//We will store the reading that happened in IN0,1,or 2 in here (its like old_in0 saying "how you feelin' IN0?", and IN0 says "dude, i am HIGH, (or LOW)" 
//then old_in0 gets on IN0's vibe and gets HIGH, (or LOW), then IN0 wanders off and does something else, and the cycle repeats.
int old_in0 = 0;
int old_in1 = 0;
int old_in2 = 0;

//This is kinda similar to the above set except way more confusing, they are all asking each other how they feel on a scale of 0-5 then vibing appropriatley.
int last_edge = 0;
int last_edge1 = 0;
int last_edge2 = 0;

//Saying here is a place for the direction we will give it some other value later
int dir = 0;

void setup() {

  //We told the program what PIN_0 was at up near the top, now we are telling the program that stuff will be coming in PIN_0 (in this case, pin 3).
  pinMode(PIN_0, INPUT); 
  pinMode(PIN_1, INPUT);
  pinMode(PIN_2, INPUT);

  pinMode(fowardLed, OUTPUT);
  pinMode(backLed, OUTPUT);

  //Begin the serial at 9600bps, on the processing end - make sure the number matches.
  Serial.begin(9600);
}

void loop(){
  
  dir = 0;  //This is the first thing that happens in the main loop it is used later to make sure serial data (fowards or backwards) is only sent when the disc is spinning.

  // Each line here is a boolean ("trueschool" or "not even aoooww"), it is the equivalent of saying  
  // if the digital pin 0 (or whatever one) is high then IN0 is High, if its not then neither is IN0  
  IN0 = digitalRead(PIN_0);
  //delay(1);
  IN1 = digitalRead(PIN_1);
  //delay(1);
  IN2 = digitalRead(PIN_2);
  
  
  //I don't really get what is going on here, we took it out of the HDDJ joker's (http://www.instructables.com/id/HDDJ_Turning_an_old_hard_disk_drive_into_a_rotary/) code, Its saying stuff like, if one pin is HIGH and the other pins are LOW (sucks to be the other pins)
  // then it figures out that its either going fowards or backwards somehow.  If you could explain it to me, please do so in the comments, the best "its like saying" wins.
  if(IN0 && !old_in0){
    if(last_edge == 5 && last_edge1 == 4 && last_edge2 == 3){
      dir=1;
    }
    else if(last_edge == 1 && last_edge1 == 2 && last_edge2 == 3){
      dir=-1;
       }
    old_in0 = IN0;
    last_edge2 = last_edge1;
    last_edge1 = last_edge;
    last_edge = 0;
  }
  else if(!IN2 && old_in2){
    if(last_edge == 0 && last_edge1 == 5 && last_edge2 == 4){
      dir=1;
    }
    else if(last_edge == 2 && last_edge1 == 3 && last_edge2 == 4){
      dir=-1;
    }
    old_in2 = IN2;
    last_edge2 = last_edge1;
    last_edge1 = last_edge;
    last_edge = 1;
  }
  else if(IN1 && !old_in1){
    if(last_edge == 1 && last_edge1 == 0 && last_edge2 == 5){
      dir=1;
    }
    else if(last_edge == 3 && last_edge1 == 4 && last_edge2 == 5){
      dir=-1;
    }
    old_in1 = IN1;
    last_edge2 = last_edge1;
    last_edge1 = last_edge;
    last_edge = 2;
  }
  else if(!IN0 && old_in0){
    if(last_edge == 2 && last_edge1 == 1 && last_edge2 == 0){
      dir=1;
    }
    else if(last_edge == 4 && last_edge1 == 5 && last_edge2 == 0){
      dir=-1;
    }
    old_in0 = IN0;
    last_edge2 = last_edge1;
    last_edge1 = last_edge;
    last_edge = 3;
  }
  else if(IN2 && !old_in2){
    if(last_edge == 3 && last_edge1 == 2 && last_edge2 == 1){
      dir=1;
    }
    else if(last_edge == 5 && last_edge1 == 0 && last_edge2 == 1){
      dir=-1;
    }
    old_in2 = IN2;
    last_edge2 = last_edge1;
    last_edge1 = last_edge;
    last_edge = 4;
  }
  else if(!IN1 && old_in1){
    if(last_edge == 4 && last_edge1 == 3 && last_edge2 == 2){
      dir=1;
    }
    else if(last_edge == 0 && last_edge1 == 1 && last_edge1 == 2){
      dir=-1;
    }
    old_in1 = IN1;
    last_edge2 = last_edge1;
    last_edge1 = last_edge;
    last_edge = 5;
  }

  produceFeedback(dir); // this tells the loop to run "void produceFeedback, we could just paste the contents of voidFeedback in here, but seperating it out makes it easier to read/modify
}

//This Function Outputs the LED values (the delay and fowardLed/backLed Low is to turn Led back off).  The function also sends via Serial.print a > or < which we use in processing.
//The if (dir != 0) part is so that nothing is sent when the HDD(stepper motor) is not moving.
void produceFeedback(int dir) {
  if (dir != 0){

    if (dir==1){
      digitalWrite(fowardLed, HIGH);
      Serial.print('>');
      delay(1);
      digitalWrite(fowardLed, LOW);
    }
    else if (dir==-1) {
      digitalWrite(backLed, HIGH);
      Serial.print('<');
      delay(1);
      digitalWrite(backLed, LOW);
    }
    
   //Useful for seeing what is goin on.
   // Serial.print(last_edge);
   // Serial.print(',');
   // Serial.print(last_edge1);
   // Serial.print(',');
   // Serial.println(last_edge2);
  
  }
   
}
